class Events {
  public list: any = {}
  constructor() {
    this.list = {}
  }
  // 订阅方法
  public listen(key: string, fn: any) {
    if (!this.list[key]) {
      this.list[key] = []
    }
    this.list[key].push(fn)
  }
  // 触发方法
  public trigger(...args: any[]) {
    let key: any = [].shift.call(args)
    let fns = this.list[key]
    fns && fns.length && fns.forEach((item: any) => {
      item.apply(this, args)
    })
  }
  // 移除订阅事件
  public remove(key: string, fn?: any) {
    // 移除所有事件
    if (!key) {
      this.list = []
    }
    let fns = this.list[key]
    if (!fns) {
      return false
    }
    if (!fn) {
      fns && (fns.length = 0)
    }
    fns = fns.filter((item: any) => item !== fn)
  }
}

export default new Events()
